﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sbook.AdminIstrator
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Data.Mysession.isLogin)
                {
                    showFormEdit(Request.QueryString["UId"]);
                    ShowListUser();
                }
                else
                {
                    Response.Redirect("/AdminIstrator/Login.aspx");
                }
            }
        }

        private void ShowListUser()
        {
            List<Data.UserInfo> listUser = Business.UserService.User_GetByTop("", "isAdmin != 1", "");
            int pageSize = 3;
            int pages = listUser.Count / pageSize;
            if (listUser.Count % pageSize > 0) pages++;
            int currentPage = 1; // trang hien tai
            if (pages > 1)
            {
             
                if (Request.QueryString["page"] != null)
                //if(RouteData.Values["page"]!=null)
                {
                    try
                    {
                        currentPage = Convert.ToInt32(Request.QueryString["page"]);
                        //currentPage = Convert.ToInt32(RouteData.Values["page"]);
                    }
                    catch
                    {
                        Response.Redirect("/AdminIstrator/UserManagement.aspx");
                        currentPage = 1;
                    }
                }

                if (currentPage <= 1)
                {
                    ltrPaging.Text = "<span class=\"disabled\"><< prev</span>";

                }
                else
                {

                    ltrPaging.Text = "<a href=\"/AdminIstrator/UserManagement.aspx?page=" + (currentPage - 1) + "\"><< prev</a>";

                }

                for (int i = 1; i <= pages; i++)
                {

                    if ((i > 3) && (currentPage - i > 3 && currentPage - i < 10 || (currentPage - i > 12)) || (i - currentPage > 3 && i - currentPage < 10 || (i - currentPage > 12)) && i < pages - 2)
                    {
                        continue;
                    }
                    if (currentPage == i)
                        ltrPaging.Text += "<span class=\"current\">" + i + "</span>";
                    else
                    {

                        ltrPaging.Text += "<a href=\"/AdminIstrator/UserManagement.aspx?page=" + i + "\">" + i + "</a>";

                    }


                }

                if (currentPage >= pages)
                {
                    ltrPaging.Text += "<span class=\"disabled\">next >></span>";

                }
                else
                {

                    ltrPaging.Text += "<a href=\"/AdminIstrator/UserManagement.aspx?page=" + (currentPage + 1) + "\">next >></a>";

                }

                if (currentPage <= 0)
                {
                    currentPage = 1;
                    Response.Redirect("/AdminIstrator/UserManagement.aspx");
                }
            }
          
            rpUser.DataSource = Business.UserService.User_Paging(currentPage.ToString(),pageSize.ToString());
            rpUser.DataBind();

            for (int i = 0; i < rpUser.Items.Count; i++)
            {
                Label lblStatus = (Label)rpUser.Items[i].FindControl("lblStatus");
                DropDownList drdlStatus = (DropDownList)rpUser.Items[i].FindControl("drdlStatus");
                if (lblStatus.Text.Equals("True")) drdlStatus.SelectedIndex = 0;
                else drdlStatus.SelectedIndex = 1;
            }
        }

        private void showFormEdit(string UId)
        {
            if (UId != null)
            {
                pnDetail.Visible = true;
                List<Data.UserInfo> listUser = Business.UserService.User_GetById(UId);
                txtName.Text = listUser[0].Fullname;
                txtProve.Text = listUser[0].Provenumber;
                txtEmail.Text = listUser[0].Email;
                txtPhone.Text = listUser[0].Phone;
                txtClass.Text = listUser[0].Class;
                txtAddress.Text = listUser[0].Address;
                txtUniversity.Text = listUser[0].University;
                rblStatus.SelectedIndex = (listUser[0].Status.Equals("True")) ? 0 : 1;
                lblAdmin.Text = listUser[0].isAdmin;
                lblPass.Text = listUser[0].Pass;
                lblUserName.Text = listUser[0].Username;
            }
            else pnDetail.Visible = false;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserManagement.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Data.UserInfo us = new Data.UserInfo();
            us.UId = Request.QueryString["UId"];
            us.Fullname = txtName.Text;
            us.Provenumber = txtProve.Text;
            us.Email = txtEmail.Text;
            us.Phone = txtPhone.Text;
            us.Class = txtClass.Text;
            us.University = txtUniversity.Text;
            us.Address = txtAddress.Text;
            us.Status = (rblStatus.SelectedIndex == 0) ? "True" : "False";
            us.isAdmin = lblAdmin.Text;
            us.Pass = lblPass.Text;
            us.Username = lblUserName.Text;
            Business.UserService.User_Update(us);

            if (Request.QueryString["page"] != null) Response.Redirect("UserManagement.aspx?page=" + Request.QueryString["page"]);
            else Response.Redirect("UserManagement.aspx");
        }

        protected void rpUser_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.Equals("Edit"))
            {
                if (Request.QueryString["page"] != null) Response.Redirect("UserManagement.aspx?page=" + Request.QueryString["page"] + "&UId=" + e.CommandArgument);
                else Response.Redirect("UserManagement.aspx?UId=" + e.CommandArgument);
            }
        }

        protected void drdlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < rpUser.Items.Count; i++)
            {
                DropDownList drdl = (DropDownList)rpUser.Items[i].FindControl("drdlStatus");
                Label Id = (Label)rpUser.Items[i].FindControl("lblUId");
                if (drdl.SelectedIndex == 0)
                {
                    Business.UserService.User_UpdateStatus(Id.Text, "1");
                }
                else
                {
                    Business.UserService.User_UpdateStatus(Id.Text, "0");
                }
            }
        }

    }
}